package org.example.managementsystem.controller;

import org.example.managementsystem.model.DeviceRepair;
import org.example.managementsystem.model.DeviceRepairReview;
import org.example.managementsystem.service.DeviceRepairService;
import org.example.managementsystem.service.DeviceRepairReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备报修与审核表现层
 * @author 王振祎
 * @date 2025-05-21
 */
@RestController
@RequestMapping("/api/device/repairs")
public class DeviceRepairController {
    private final DeviceRepairService deviceRepairService;
    private final DeviceRepairReviewService deviceRepairReviewService;

    @Autowired
    public DeviceRepairController(DeviceRepairService deviceRepairService, DeviceRepairReviewService deviceRepairReviewService) {
        this.deviceRepairService = deviceRepairService;
        this.deviceRepairReviewService = deviceRepairReviewService;
    }

    /**
     * 提交设备报修
     */
    @PostMapping("/submit")
    public ResponseEntity<String> submitRepair(@RequestBody DeviceRepair repair) {
        // 保证数据安全，设置申请状态为待审核
        repair.setIsApproved(false);
        repair.setStatus(false);
        deviceRepairService.submitRepair(repair);
        return ResponseEntity.ok("设备报修提交成功");
    }

    /**
     * 审核设备报修并记录审核信息
     */
    @PutMapping("/review")
    public ResponseEntity<String> reviewRepair(@RequestBody DeviceRepairReview review) {
        deviceRepairReviewService.reviewRepair(review);
        return ResponseEntity.ok("设备报修审核成功");
    }

    /**
     * 更新报修信息
     */
    @PutMapping("/update")
    public ResponseEntity<String> updateRepair(@RequestBody DeviceRepair repair) {
        deviceRepairService.updateRepair(repair);
        return ResponseEntity.ok("设备报修更新成功");
    }

    /**
     * 获取所有报修
     */
    @GetMapping("/getRepair")
    public ResponseEntity<List<DeviceRepair>> getAllRepairs() {
        List<DeviceRepair> repairs = deviceRepairService.getAllRepairs();
        return ResponseEntity.ok(repairs);
    }

    /**
     * 获取所有未审核的报修
     */
    @GetMapping("/getUnreviewedRepair")
    public ResponseEntity<List<DeviceRepair>> getAllUnreviewedRepairs() {
        List<DeviceRepair> repairs = deviceRepairService.getAllUnreviewedRepairs();
        return ResponseEntity.ok(repairs);
    }

    /**
     * 根据ID获取报修
     */
    @GetMapping("/getRepairById/{repairId}")
    public ResponseEntity<DeviceRepair> getRepairById(@PathVariable int repairId) {
        DeviceRepair repair = deviceRepairService.getRepairById(repairId);
        return ResponseEntity.ok(repair);
    }

    /**
     * 根据ID获取审核记录
     */
    @GetMapping("/getReviewById/{repairId}")
    public ResponseEntity<DeviceRepairReview> getReviewById(@PathVariable int repairId) {
        DeviceRepairReview review = deviceRepairReviewService.getReviewById(repairId);
        return ResponseEntity.ok(review);
    }

    /**
     * 根据ID删除报修
     */
    @DeleteMapping("/delete/{repairId}")
    public ResponseEntity<String> deleteRepair(@PathVariable int repairId) {
        deviceRepairService.deleteRepair(repairId);
        return ResponseEntity.ok("设备报修删除成功");
    }

    /**
     * 根据用户ID获取报修记录
     */
    @GetMapping("/getRepairByUserId/{userId}")
    public ResponseEntity<List<DeviceRepair>> getRepairByUserId(@PathVariable int userId) {
        List<DeviceRepair> repairs = deviceRepairService.getRepairByUserId(userId);
        return ResponseEntity.ok(repairs);
    }

    /**
     * 根据设备ID获取报修记录
     */
    @GetMapping("/getRepairByDeviceId/{deviceId}")
    public ResponseEntity<List<DeviceRepair>> getRepairByDeviceId(@PathVariable int deviceId) {
        List<DeviceRepair> repairs = deviceRepairService.getRepairByDeviceId(deviceId);
        return ResponseEntity.ok(repairs);
    }
}